package com.xc.disk.web.rest;

import com.xc.disk.bean.PagingBean;
import com.xc.disk.bean.QueryBean;
import com.xc.disk.bean.SafetyChainBean;
import com.xc.disk.dto.PagingDto;
import com.xc.disk.entity.SafetyChainEntity;
import com.xc.disk.info.TokenInfo;
import com.xc.disk.service.SafetyChainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>项目名称: Stars</p>
 * <p>描述: 安全链接rest接口</p>
 * <P>创建人:小程 </p>
 * <P>创建人邮箱: 779943476@qq.com</p>
 * <p>创建时间: 2019/12/19</p>
 *
 * @version v1.0
 */
@RestController
public class SafetyChainRest {

    @Autowired
    private SafetyChainService safetyChainService;

    /**
     * 分页查询
     *
     * @param token     用户token信息
     * @param size      每页多少条数据
     * @param current   第几页
     * @param queryBean 排序模糊查询
     * @return 查询结果
     */
    @GetMapping("/safety/chain/paging/{size}/{current}")
    public PagingDto<SafetyChainEntity> getSafetyChainPaging(TokenInfo token,
                                                             @PathVariable Integer size,
                                                             @PathVariable Integer current,
                                                             @ModelAttribute QueryBean queryBean,
                                                             @RequestParam(required = false) String url) {
        PagingBean<SafetyChainEntity> pagingBean = new PagingBean<>(queryBean);
        pagingBean.setCurrent(current);
        pagingBean.setSize(size);
        SafetyChainEntity safetyChainEntity = new SafetyChainEntity();
        safetyChainEntity.setUrl(url);
        pagingBean.setData(safetyChainEntity);
        return safetyChainService.getSafetyChainPaging(token, pagingBean);
    }


    /**
     * 创建安全链接
     *
     * @param token           用户token信息
     * @param safetyChainBean 创建参数
     * @return 创建结果
     */
    @PostMapping("/safety/chain")
    public SafetyChainEntity createSafetyChain(TokenInfo token, @RequestBody SafetyChainBean safetyChainBean) {
        return safetyChainService.createSafetyChain(token.getUserId(), safetyChainBean);
    }

    /**
     * 修改安全链接
     *
     * @param token           用户token信息
     * @param safetyChainBean 修改参数
     * @return 修改结果
     */
    @PutMapping("/safety/chain/{id}")
    public SafetyChainEntity updateSafetyChain(TokenInfo token, @PathVariable String id, @RequestBody SafetyChainBean safetyChainBean) {
        return safetyChainService.updateSafetyChain(token.getUserId(), id, safetyChainBean);
    }

    /**
     * 删除安全链接
     *
     * @param token 用户token信息
     * @param ids   安全链接id集合
     */
    @DeleteMapping("/safety/chain/{ids}")
    public void deleteSafetyChain(TokenInfo token, @PathVariable List<String> ids) {
        safetyChainService.deleteSafetyChain(token.getUserId(), ids);
    }
}
